# # plot_from_dat_lines.py
# import matplotlib.pyplot as plt

# # 读取.dat文件中的数据
# with open('data.dat', 'r') as file:
#     lines = file.readlines()

# # 将每一行的数据转换为整数
# data = [int(line.strip()) for line in lines]

# # 创建横坐标序列（从1开始）
# x = list(range(1, len(data) + 1))

# # 绘制曲线
# plt.plot(x, data)
# plt.xlabel('Time')
# plt.ylabel('Data')
# plt.title('Data over Time')
# plt.show()


# # plot_multiple_columns_from_dat.py
# import matplotlib.pyplot as plt

# # 读取.dat文件中的数据
# with open('data.dat', 'r') as file:
#     lines = file.readlines()

# # 解析数据并获取列数
# data = [list(map(float, line.strip().split())) for line in lines]
# num_columns = len(data[0])

# print(f"Number of columns in data.dat: {num_columns}")

# # 创建并绘制每一列的图像
# for column in range(num_columns):
#     # 提取当前列的数据
#     column_data = [row[column] for row in data]

#     # 创建新的图像
#     plt.figure()

#     # 绘制当前列的数据
#     plt.plot(column_data)
#     plt.xlabel('Time')
#     plt.ylabel(f'Data in Column {column + 1}')
#     plt.title(f'Data in Column {column + 1} over Time')

# # 显示所有图像
# plt.show()


# plot_multiple_columns_from_dat_with_choice.py
import matplotlib.pyplot as plt

# 读取.dat文件中的数据
print("Reading data from data.dat...")
with open('data.dat', 'r') as file:
    lines = file.readlines()

# 解析数据并获取列数
print("Parsing data and determining number of columns...")
data = [list(map(float, line.strip().split())) for line in lines]
num_columns = len(data[0])

# 打印列数到控制台
print(f"Number of columns in data.dat: {num_columns}")

# 提示用户选择绘制方式
# choice = input("Enter 1 to plot all columns together, or 2 to plot each column separately: ")
choice = "1"

# 根据用户选择绘制图像
if choice == "1":
    # 创建新的图像
    plt.figure()

    # 绘制所有列的数据
    for column in range(num_columns):
        # 提取当前列的数据
        column_data = [row[column] for row in data]

        # 绘制当前列的数据
        plt.plot(column_data, label=f'Column {column + 1}')

    plt.xlabel('Time')
    plt.ylabel('Data')
    plt.title('All Columns Together')
    plt.legend()
    plt.show()
elif choice == "2":
    # 创建并绘制每一列的图像
    print(f"Plotting {num_columns} columns separately...")
    for column in range(num_columns):
        # 提取当前列的数据
        column_data = [row[column] for row in data]

        # 创建新的图像
        plt.figure()

        # 绘制当前列的数据
        plt.plot(column_data)
        plt.xlabel('Time')
        plt.ylabel(f'Data in Column {column + 1}')
        plt.title(f'Data in Column {column + 1} over Time')

    # 显示所有图像
    print("Displaying plots...")
    plt.show()
else:
    print("Invalid choice. Please enter 1 or 2.")
